package com.yutian.springbootvue.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // https://www.cnblogs.com/liaojie970/p/8298271.html
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(3000000);
        registry.addResourceHandler("/**.html")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(1);
    }

    /**
     * 该过滤器用于设置user，需前置
     */
    @Bean
    public FilterRegistrationBean traceUserFilter() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new TraceUserFilter());
        filterRegistration.setName("traceUserFilter");
        filterRegistration.addUrlPatterns("/*");
        filterRegistration.setOrder(1);
        return filterRegistration;
    }

}
